<template>
  <el-breadcrumb class="app-breadcrumb" separator="/">
    <transition-group name="breadcrumb">
      <el-breadcrumb-item v-for="(item, index) in levelList" :key="item.path">
        <span
          v-if="
            item.redirect === 'noRedirect' ||
            (!item.redirect && item.path !== '/index') ||
            index == levelList.length - 1
          "
          class="no-redirect"
          >{{ item.meta.title }}</span
        >
        <a v-else @click.prevent="handleLink(item)">{{ item.meta.title }}</a>
      </el-breadcrumb-item>
    </transition-group>
  </el-breadcrumb>
</template>

<script>
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      levelList: [],
    };
  },
  computed: {
    ...mapGetters(["sidebarRouters"]),
  },
  watch: {
    $route(route) {
      // if you go to the redirect page, do not update the breadcrumbs
      if (route.path.startsWith("/redirect/")) {
        return;
      }
      this.getBreadcrumb();
    },
  },
  created() {
    this.getBreadcrumb();
  },
  methods: {
    /**
     * 路由不支持二级以上菜单缓存，已将二级以上的路由拍平。即不能依靠$route.matched属性来定义面包屑
     */
    getBreadcrumb() {
      // only show routes with meta.title
      let matched = this.$route.matched.filter(
        (item) => item.meta && item.meta.title
      );

      this.levelList = this.buildLevels(matched, this.sidebarRouters).filter(
        (item) => item.meta && item.meta.title && item.meta.breadcrumb !== false
      );
    },

    /**
     * 构建节点信息：路由只有两层
     * @param {*} matched 当前路由的所有嵌套路由记录
     * @param {*} routers sidebarRouters
     * @return {*} levels 构建的返回节点
     */
    buildLevels(matched, routers = []) {
      let levels = [];

      if (Array.isArray(matched) && matched.length > 0) {
        const firstRouter = matched[0];
        if (!this.isDashboard(firstRouter)) {
          levels.push({ path: "/index", meta: { title: "首页" } });
        }

        if (matched.length === 1) {
          levels.push(firstRouter);
        } else if (matched.length > 1) {
          const [firstRouter, twoRouter] = matched;
          const rootRouter = routers.find((r) => {
            return r.path === firstRouter.path;
          });
          if (
            rootRouter &&
            Array.isArray(rootRouter.children) &&
            rootRouter.children.length > 0
          ) {
            const childs = innerFunc(twoRouter.path, rootRouter.children);
            if (Array.isArray(childs) && childs.length > 0) {
              levels = levels.concat(childs);
            }
          }
        }
      }

      function innerFunc(path, routers = [], levels = []) {
        for (let item of routers) {
          if (item.path === path) {
            levels.push(item);
            if (levels.length === 1) return levels;
            return true;
          }
          if (Array.isArray(item.children) && item.children.length > 0) {
            levels.push(item);
            const find = innerFunc(path, item.children, levels);
            if (find) {
              return levels;
            }
            levels.pop();
          }
        }
        return false;
      }
      return levels;
    },
    isDashboard(route) {
      const name = route && route.name;
      if (!name) {
        return false;
      }
      return name.trim() === "Index";
    },
    handleLink(item) {
      const { redirect, path } = item;
      if (redirect) {
        this.$router.push(redirect);
        return;
      }
      this.$router.push(path);
    },
  },
};
</script>

<style lang="scss" scoped>
.app-breadcrumb.el-breadcrumb {
  display: inline-block;
  font-size: 16px;
  line-height: 50px;
  margin-left: 8px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;

  .no-redirect {
    color: #252631;
    cursor: text;
  }
}
</style>
